﻿Imports System.Data.SqlClient
Imports System.Data

Public Class EditContact
    Inherits System.Web.UI.Page
    Dim myConnection As SqlConnection

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        myConnection = New SqlConnection("Data Source= (local);" _
                                         & "Database=Phonebook;Integrated Security=True")
    End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim updateSql As SqlCommand
        Dim updateString As String

        'Dim queryString As String = "select password from login where user = @user;"
        'Dim querySql As SqlCommand = New SqlCommand(queryString, myConnection)
        'querySql.Parameters.Add(New SqlParameter("@user", SqlDbType.NChar, 20))
        'querySql.Parameters("@user").Value = "gwang8"
        'Try
        '    myConnection.Open()
        '    Dim reader As SqlDataReader = querySql.ExecuteReader()
        '    If (reader.HasRows) Then
        '        reader.Read()
        '        If (reader(0).Equals("yahasoft2010")) Then
        '            Session("user") = User
        '            Response.BufferOutput = True
        '            Response.Redirect("Home.aspx")
        '        Else
        '            Message.Text = "The password is not valid. Please try again."
        '        End If
        '    Else
        '        Message.Text = "The user doesn't exist."
        '    End If
        '    reader.Close()
        'Catch ex As Exception
        '    Message.Text = ex.Message
        '    myConnection.Close()
        'End Try

        updateString = "update contacts set cell = @cell where cell = @oldCell;"
        updateSql = New SqlCommand(updateString, myConnection)
        updateSql.Parameters.Add(New SqlParameter("@cell", SqlDbType.NChar, 50))
        updateSql.Parameters("@cell").Value = Cell.Text
        updateSql.Parameters.Add(New SqlParameter("@oldCell", SqlDbType.NChar, 50))
        updateSql.Parameters("@oldCell").Value = CType(Session.Item("cell"), String)
        Try
            myConnection.Open()
            updateSql.ExecuteNonQuery()
            Message.ForeColor = Drawing.Color.Green
            Message.Text = "Update Executed"
        Catch ex As Exception

        End Try
        myConnection.Close()
        Response.BufferOutput = False
        Response.Redirect("Home.aspx")
    End Sub
End Class